//
//  RBGServiceChargment.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 29/05/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGServiceChargement.h"
#import "RBGConfigurationClientService.h"
#import "Constantes.h"
#import "RBGParserDonnees.h"
#import "XMLParser.h"

@interface RBGServiceChargement ()<ServiceDelegate>{
    int nbTypeCategoriesRecu;
    NSMutableArray  * typesCategoriesList;
    BOOL waTmp;
}

@end

@implementation RBGServiceChargement

-(void) envoieJeton:(NSString*)jeton{
    
    RBGConfigurationClientService* service = [[RBGConfigurationClientService alloc]init];
    
    NSMutableDictionary * arguments = [[NSMutableDictionary alloc]init];
    
    [arguments setObject:jeton forKey:JETON_SMARTPHONE_CLIENT_JETON];
    
    [service getDonnneesByUrl:URL_JETON andArguments:arguments];
}


-(void) getParametreClient{
    
    self.service = [[RBGConfigurationClientService alloc]init];
    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = parametre;
    [self.service getDonnneesByUrl:URL_PARAMETRE andArguments:nil];
}

-(void) chargerEtats{
    
    self.service = [[RBGConfigurationClientService alloc]init];
    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = [NSString stringWithFormat:@"%01d",etats];
    [self.service getDonnneesByUrl:URL_ETATS andArguments:nil];
    
}

-(void) chargerLieux{
    self.service = [[RBGConfigurationClientService alloc]init];

    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = [NSString stringWithFormat:@"%01d",lieux];
    [self.service getDonnneesByUrl:URL_LIEUX andArguments:nil];
    
}

-(void) chargerMarquesDistribuees{
    self.service = [[RBGConfigurationClientService alloc]init];

    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = [NSString stringWithFormat:@"%01d",marquedistribuee];
    [self.service getDonnneesByUrl:URL_MARQUES_DISTRIBUEES andArguments:nil];
    
}


-(void) chargerDepartements{
    self.service = [[RBGConfigurationClientService alloc]init];

    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = [NSString stringWithFormat:@"%01d",departements];
    [self.service getDonnneesByUrl:URL_DEPARTEMENTS andArguments:nil];
    
}

-(void) chargerEnergies{
    self.service = [[RBGConfigurationClientService alloc]init];

    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = [NSString stringWithFormat:@"%01d",energies];
    [self.service getDonnneesByUrl:URL_ENERGIES andArguments:nil];
    
}

-(void) chargerServices{
    self.service = [[RBGConfigurationClientService alloc]init];

    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = [NSString stringWithFormat:@"%01d",services];
    [self.service getDonnneesByUrl:URL_SERVICES andArguments:nil];
    
}

-(void) chargerRegions{
    self.service = [[RBGConfigurationClientService alloc]init];

    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = [NSString stringWithFormat:@"%01d",regions];
    [self.service getDonnneesByUrl:URL_REGIONS andArguments:nil];
    
}

-(void) chargerTypesAnnonces{
    self.service = [[RBGConfigurationClientService alloc]init];

    self.service.delegate = self;
    
    // On récupére les paramétres
    self.service.typeService = [NSString stringWithFormat:@"%01d",typesAnnonces];
    [self.service getDonnneesByUrl:URL_TYPES_ANNONCES andArguments:nil];
}

-(void) chargerTypesCategories:(BOOL)wa{
    
    typesCategoriesList = [[NSMutableArray alloc]init];
    nbTypeCategoriesRecu = 1;
    
    self.service = [[RBGConfigurationClientService alloc]init];
    self.service.delegate = self;
    
    if(wa)
        self.service.typeService = [NSString stringWithFormat:@"%01d",typesCategories];
    else
        self.service.typeService = [NSString stringWithFormat:@"%01d",typesCategoriesTOUTES];
    
    waTmp = wa;
    [self lancerTypeCategorie:nbTypeCategoriesRecu andWa:wa andService:self.service];
    
}

-(void) lancerTypeCategorie:(int)nb
                      andWa:(BOOL)wa
                 andService:(RBGConfigurationClientService *)service
{
    
    NSMutableDictionary * arguments = [[NSMutableDictionary alloc]init];
    
    [arguments addEntriesFromDictionary:@{
                                          CATEGORIES_TYPE_ID:[NSString stringWithFormat:@"%01d",nb]
                                          }];
    
    if(wa)
        [arguments addEntriesFromDictionary:@{
                                              MARQUES_POUR:[NSString stringWithFormat:@"%01d",1]
                                              }];
    
    [service getDonnneesByUrl:URL_TYPES_CATEGORIES andArguments:arguments];
    
}



-(void) chargerMarquesBateauType:(NSString *)idType
                     andIdClient:(NSString *)idClient
                           andWa:(BOOL)wa{
    
    self.service = [[RBGConfigurationClientService alloc]init];
    self.service.delegate = self;
    
    NSMutableDictionary * arguments = [[NSMutableDictionary alloc]init];
    
    if(wa)
        [arguments addEntriesFromDictionary:@{
                                              MARQUES_POUR:[NSString stringWithFormat:@"%01d",1]
                                              }];
    
    if(idType != nil)
        [arguments addEntriesFromDictionary:@{
                                              MARQUES_ID_TYPE:idType
                                              }];
    
    if(idClient != nil)
        [arguments addEntriesFromDictionary:@{
                                              MARQUES_ID_TYPE:idClient
                                              }];
    
    if(idType != nil){
    
        // On récupére les paramétres
        if([idType isEqualToString:BATEAU_A_MOTEUR]){
            if(wa)
                self.service.typeService = [NSString stringWithFormat:@"%01d",marquesBateauxAMoteur];
            else
                self.service.typeService = [NSString stringWithFormat:@"%01d",TOUTESmarquesBateauxAMoteur];
        }
        
        if([idType isEqualToString:VOILIER]){
            if(wa)
                self.service.typeService = [NSString stringWithFormat:@"%01d",marquesVoilier];
            else
                self.service.typeService = [NSString stringWithFormat:@"%01d",TOUTESmarquesVoilier];
        }
        
        if([idType isEqualToString:PNEU]){
            if(wa)
                self.service.typeService = [NSString stringWithFormat:@"%01d",marquesPneu];
            else
                self.service.typeService = [NSString stringWithFormat:@"%01d",TOUTESmarquesPneu] ;
        }
        
    }else{
        self.service.typeService = [NSString stringWithFormat:@"%01d",toutesMarques]  ;
    }
    
    
    [self.service getDonnneesByUrl:URL_MARQUES andArguments:arguments];
}

-(void) chargerMarquesMoteurs:(NSString *)idClient
                        andWA:(BOOL)wa{
    
    self.service = [[RBGConfigurationClientService alloc]init];
    self.service.delegate = self;
    
    NSMutableDictionary * arguments = [[NSMutableDictionary alloc]init];
    
    if(wa)
        [arguments addEntriesFromDictionary:@{
                                              MARQUES_POUR:[NSString stringWithFormat:@"%01d",1]
                                              }];
    
    if(idClient != nil)
        [arguments addEntriesFromDictionary:@{
                                              MARQUES_POUR_CLIENT:idClient
                                              }];

    if(wa)
        self.service.typeService = [NSString stringWithFormat:@"%01d",marquesMoteur];
    else
        self.service.typeService = [NSString stringWithFormat:@"%01d",TOUTESmarquesMoteur];
    
    [self.service getDonnneesByUrl:URL_MARQUES_MOTEUR andArguments:arguments];

}




-(void) chargerModeles:(NSString *)idMarques
             andIdType:(NSString *)idType
                 andWa:(BOOL)wa
           andIdClient:(NSString *)idClient
{
    
    self.service = [[RBGConfigurationClientService alloc]init];
    self.service.delegate = self;
    
    NSMutableDictionary * arguments = [[NSMutableDictionary alloc]init];
    
    if(idMarques != nil)
    [arguments addEntriesFromDictionary:@{
                                          MODELES_ID_MARQUE:idMarques
                                          }];
    if(wa)
        [arguments addEntriesFromDictionary:@{
                                              MODELES_POUR:[NSString stringWithFormat:@"%01d",1]
                                              }];
    
    if(idType != nil)
        [arguments addEntriesFromDictionary:@{
                                              MODELES_ID_TYPE:idType
                                              }];
    
    if(idClient != nil)
        [arguments addEntriesFromDictionary:@{
                                              MODELES_ID_CLIENT:idClient
                                              }];
    
        self.service.typeService = [NSString stringWithFormat:@"%01d",modeles];

    
    [self.service getDonnneesByUrl:URL_MODELES andArguments:arguments];
    
}

- (void) receiveDonneesService:(id)donnees fromService:(int)typeService{
    
    if(([self.service.typeService integerValue] == typesCategories || [self.service.typeService integerValue] == typesCategoriesTOUTES)){
        if(nbTypeCategoriesRecu != 8 ){
            
            nbTypeCategoriesRecu ++;
            [typesCategoriesList addObject:donnees];
            self.service = [[RBGConfigurationClientService alloc]init];
            self.service.delegate = self;
            self.service.typeService = [NSString stringWithFormat:@"%01d",typeService];
            [self lancerTypeCategorie:nbTypeCategoriesRecu andWa:waTmp andService:self.service];
            
        }else{
            
            [typesCategoriesList addObject:donnees];
            [self.delegate receiveDonneesChargement:typesCategoriesList andType:typeService];
        }
 
    }
    
    if([self.service.typeService integerValue] != typesCategories && [self.service.typeService integerValue] != typesCategoriesTOUTES)
        [self.delegate receiveDonneesChargement:donnees andType:[self.service.typeService integerValue]];
    
}

- (void) receiveErreurService:(int)statut{
    [self.delegate receiveErreurChargement:statut];
}


@end
